﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Metadata;
using RetrieveEntityDetails;
using RetrieveEntityDetails.Utility;

namespace RetrieveEntityDetails.BusinessLogic
{
    class CSVFileWriter
    {
        public string FolderPath;
        public string EntityName;
        public EntityMetadata EntityMetadataDataSet;

        public CSVFileWriter()
        {
            EntityMetadataDataSet = new EntityMetadata();                      
        }

        private string InsertComma(string String1, string String2)
        {
            return String1 + ":" + String2 + ",";
        }

        public bool Write(DataRow[] EntityRows, DataRow[] PickListRows,DataRow[] LookUpRows, DataRow[] StateCodeRows, DataRow[] StatusCodeRows)
        {
            EntityName += " - " + EntityRows[0][EntityMetadataDataSet.AttributeMetadataTable.EntitySchemaNameColumn.ToString()].ToString();
            string PrimaryKeyAttributeSchemaName = string.Empty;
            FileSystemHelper File = new FileSystemHelper();
            File.FileNameWithPath = FolderPath + "\\" + EntityName + ".csv";
            File.FileNameWithPath = File.FileNameWithPath.Replace("/", "");
            //Heading of the CSV File
            File.Write(EntityTableColumns.AttributeName.ToString()
                + "," + EntityTableColumns.AttributeSchemaName.ToString()
                + "," + EntityTableColumns.AttributeType.ToString()
                + ",AttributeValues", false);

            //Contents of the CSV File
            foreach (DataRow RowItem in EntityRows)
            {
                string AttributeValues = string.Empty;
                AttributeValues = "\"";
                foreach (DataRow PickListItem in PickListRows)
                {
                    if (PickListItem[EntityMetadataDataSet.PickListMetadataTable.AttributeIDColumn.ToString()].ToString().ToLower() == RowItem[EntityMetadataDataSet.AttributeMetadataTable.AttributeIDColumn.ToString()].ToString().ToLower())
                    {
                        AttributeValues += InsertComma(PickListItem[PickListTableColumns.PickListOptionValue.ToString()].ToString(), PickListItem[PickListTableColumns.PickListOptionDisplayName.ToString()].ToString());
                    }
                }
                foreach (DataRow StateCodeItem in StateCodeRows)
                {
                    if (StateCodeItem[EntityMetadataDataSet.StateCodeTable.AttributeIDColumn.ToString()].ToString().ToLower() == RowItem[EntityMetadataDataSet.AttributeMetadataTable.AttributeIDColumn.ToString()].ToString().ToLower())
                    {
                        AttributeValues += InsertComma(StateCodeItem[EntityMetadataDataSet.StateCodeTable.ValueColumn.ToString()].ToString(), StateCodeItem[EntityMetadataDataSet.StateCodeTable.DisplayNameColumn.ToString()].ToString());
                    }
                }
                foreach (DataRow StatusCodeItem in StatusCodeRows)
                {
                    if (StatusCodeItem[EntityMetadataDataSet.StatusCodeTable.AttributeIDColumn.ToString()].ToString().ToLower() == RowItem[EntityMetadataDataSet.AttributeMetadataTable.AttributeIDColumn.ToString()].ToString().ToLower())
                    {
                        AttributeValues += InsertComma(StatusCodeItem[EntityMetadataDataSet.StatusCodeTable.ValueColumn.ToString()].ToString(), StatusCodeItem[EntityMetadataDataSet.StatusCodeTable.DisplayNameColumn.ToString()].ToString());
                    }
                }
                foreach (DataRow LookUpItem in LookUpRows)
                {
                    if (LookUpItem[EntityMetadataDataSet.LookupTypeTable.AttributeIDColumn.ToString()].ToString().ToLower() == RowItem[EntityMetadataDataSet.AttributeMetadataTable.AttributeIDColumn.ToString()].ToString().ToLower())
                    {
                        AttributeValues += LookUpItem[EntityMetadataDataSet.LookupTypeTable.LookupTypeColumn.ToString()].ToString();
                    }
                }
                AttributeValues += "\"";
                File.Write(RowItem[EntityTableColumns.AttributeName.ToString()].ToString()
                + "," + RowItem[EntityTableColumns.AttributeSchemaName.ToString()].ToString()
                + "," + RowItem[EntityTableColumns.AttributeType.ToString()].ToString() + "," + AttributeValues);
            }
            return true;
        }
    }

}
